package com.smartleanx.module.hrm.service.employee;

import cn.hutool.core.collection.CollUtil;
import com.smartleanx.framework.common.util.collection.CollectionUtils;
import com.smartleanx.module.hrm.context.EmployeeContextHolder;
import com.smartleanx.module.hrm.controller.admin.employee.vo.EmployeeBasicInfoRespVo;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeRegularizeRecordDO;
import com.smartleanx.module.hrm.dal.mysql.employee.EmployeeRegularizeRecordMapper;
import com.smartleanx.module.hrm.eums.employee.EmployeeStatusEnum;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;


import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.*;

/**
 * 转正记录 Service 实现类
 *
 * @author 精益大师
 */
@Service
@Validated
public class EmployeeRegularizeRecordServiceImpl implements EmployeeRegularizeRecordService {

    @Resource
    private EmployeeRegularizeRecordMapper employeeRegularizeRecordMapper;
    @Override
    public int createRegularizeRecord(EmployeeRegularizeRecordDO employeeRegularizeRecord) {
        employeeRegularizeRecord.setId(null);
        return employeeRegularizeRecordMapper.insert(employeeRegularizeRecord);
    }

    @Override
    public void updateRegularizeRecord(EmployeeRegularizeRecordDO updateObj) {
        // 校验存在
        validateEmployeeRegularizeRecordExists(updateObj.getId());
        employeeRegularizeRecordMapper.updateById(updateObj);
    }

    private void validateEmployeeRegularizeRecordExists(Long id) {
        if (employeeRegularizeRecordMapper.selectById(id) == null) {
            throw exception(EMPLOYEE_REGULARIZE_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public EmployeeRegularizeRecordDO getEmployeeRegularizeRecord(Long id) {
        return employeeRegularizeRecordMapper.selectById(id);
    }

    @Override
    public List<EmployeeRegularizeRecordDO> getRegularizeRecordList(Long employeeId){
        return employeeRegularizeRecordMapper.selectListByEmployeeId(employeeId);
    }

    @Override
    public List<EmployeeRegularizeRecordDO> getRegularizeRecordList(Collection<Long> employeeIdList){
        if (CollUtil.isEmpty(employeeIdList)) {
            return Collections.emptyList();
        }
        return employeeRegularizeRecordMapper.selectListByEmployeeId(employeeIdList);
    }

    @Override
    public List<EmployeeRegularizeRecordDO> getRegularizeRecordList(){

        List<EmployeeRegularizeRecordDO> list = employeeRegularizeRecordMapper.selectList();
        if (CollUtil.isEmpty(list)){
            return Collections.emptyList();
        }
        // 按员工分类，取最后一条
        Map<Long, List<EmployeeRegularizeRecordDO>> listMap = CollectionUtils.convertMultiMap(list, EmployeeRegularizeRecordDO::getEmployeeId);
        List<EmployeeRegularizeRecordDO> recordList = new ArrayList<>();
        for (Long key : listMap.keySet()){
            List<EmployeeRegularizeRecordDO> collect = listMap.get(key).stream().sorted(Comparator.comparing(EmployeeRegularizeRecordDO::getId).reversed()).collect(Collectors.toList());
            recordList.add(collect.get(0));
        }

        return recordList;
    }

    @Override
    public void deleteEmployeeRegularizeRecord(Long id) {
        employeeRegularizeRecordMapper.deleteById(id);
    }

}
